--
--    Health Organizer (http://sourceforge.net/projects/hor/,
--                      http://hor.sourceforge.net/)
--    Copyright (C) 2015-2016  Andrea Urbani ( matfanjol@users.sf.net )
--
--    This program is free software: you can redistribute it and/or modify
--    it under the terms of the GNU General Public License as published by
--    the Free Software Foundation, either version 3 of the License, or
--    (at your option) any later version.
--
--    This program is distributed in the hope that it will be useful,
--    but WITHOUT ANY WARRANTY; without even the implied warranty of
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--    GNU General Public License for more details.
--
--    You should have received a copy of the GNU General Public License
--    along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
-- ----------
-- To help in the import phases
ALTER TABLE "tImports"
  ADD COLUMN "tmpBigInt" bigint;

ALTER TABLE "tExaminations"
  ADD COLUMN "Highlight_Reason" character varying(45);
ALTER TABLE "tExaminations"
  ADD COLUMN "Highlight_Color" integer;
ALTER TABLE "tExaminations"
  ADD COLUMN "CopyToUsb" boolean not null default false;
DROP TABLE "tExemption";
CREATE TABLE "tExemptions"
(
  "ID" serial NOT NULL,
  "ID_Patient" integer NOT NULL,
  "Name" character varying(45) NOT NULL,
  "StartDate" date NOT NULL,
  "EndDate" date,
  "ID_Document" integer,
  "Note" text,
  CONSTRAINT "pk_tExemptions" PRIMARY KEY ("ID"),
  CONSTRAINT "fk_tExemptions_tDocuments" FOREIGN KEY ("ID_Document")
      REFERENCES "tDocuments" ("ID") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT "fk_tExemptions_tPatients" FOREIGN KEY ("ID_Patient")
      REFERENCES "tPatients" ("ID_Person") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);

-- I fix the Problems that has to be relative to the patient's pathologies
delete from "tProblems" "t1"
where not exists (Select *
from "tPatientsPathologies" "t2"
where "t2"."ID_Person" = "t1"."ID_Patient" and
"t2"."ID_Pathology" = "t1"."ID_Pathology");
ALTER TABLE "tPatientsPathologies" RENAME "ID_Person"  TO "ID_Patient";
ALTER TABLE "tProblems" drop constraint "fk_tProblems_tPathologies";
ALTER TABLE "tProblems" add
  CONSTRAINT "fk_tProblems_tPatientsPathologies" FOREIGN KEY ("ID_Patient","ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient","ID_Pathology") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE RESTRICT;

-- Drugs changes
ALTER TABLE "tDrugsNames"
   ALTER COLUMN "Name" TYPE character varying(110);
ALTER TABLE "tDrugsNames"
  ADD COLUMN "Description" character varying(150);
ALTER TABLE "tDrugsNames"
  ADD COLUMN "Price" real;
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "ID_MeasurementUnit" integer NOT NULL;
ALTER TABLE "tDrugsPrescriptions"
  ADD CONSTRAINT "fk_tDrugsPrescriptions_tMeasurementsUnits" FOREIGN KEY ("ID_MeasurementUnit")
      REFERENCES "tMeasurementsUnits" ("ID")
      ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE "tDrugsPrescriptions"
DROP COLUMN "When";
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "EveryType" char(1) NOT NULL;
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "EveryQuantity" integer not null;
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "When" char(2) NOT NULL;
ALTER TABLE "tDrugsPrescriptions"
  add CONSTRAINT "ck_tDrugsPrescriptions_EveryType" CHECK
    ("EveryType" in ('H','D','W','M','Y'));
ALTER TABLE "tDrugsPrescriptions"
  add CONSTRAINT "ck_tDrugsPrescriptions_When" CHECK
    ("When" in ('W ','BB','BD','BA','MM','LB','LD','LA','MA','DB','DD','DA','S ','E ','F ','=='));
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "WhenTime" timestamp without time zone;
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "EndWhen" char(1) not null;
ALTER TABLE "tDrugsPrescriptions"
  add CONSTRAINT "ck_tDrugsPrescriptions_EndWhen" CHECK
    ("EndWhen" in ('N','D','Q'));
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "EndQuantity" integer;
COMMENT ON COLUMN "tDrugsPrescriptions"."EveryType"
  IS 'H = every X hours
D = every X days
W = every X weeks
M = every X months
Y = every X years';
COMMENT ON COLUMN "tDrugsPrescriptions"."When"
  IS 'W = at Wake up
BB = Before Breakfast
BD = During Breakfast
BA = After Breakfast
MM = in the Middle of the Morning
LB = Before Lunch
LD = During Lunch
LA = After Lunch
MA = in the Middle of the Afternoon
DB = Before Dinner
DD = During Dinner
DA = After Dinner
S = Before to go to sleep
E = Empty stomach
F = Full stomach
== = no matter / it is the same';
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "ID_Problem" integer not null;
ALTER TABLE "tDrugsPrescriptions"
  ADD CONSTRAINT "fk_tDrugsPrescriptions_tProblems"
    foreign key ("ID_Problem")
      references "tProblems" ( "ID" );
COMMENT ON COLUMN "tDrugsPrescriptions"."EndWhen" is
'N = Never
D = at EndDate date
Q = after quantity times';
ALTER TABLE "tDrugsPrescriptions"
  ADD COLUMN "Note" text;
ALTER TABLE "tDrugsPrescriptions"
  add CONSTRAINT "ck_tDrugsPrescriptions_StartDate_EndDate" CHECK
    ("StartDate"<="EndDate");
alter table "tDrugsNames"
  RENAME "ID_Company" TO "ID_DrugCompany";
alter table "tDrugs"
  add column "ID_DrugPrescription" integer;
alter table "tDrugs"
  add constraint "fk_tDrugs_tDrugsPrescriptions"
    foreign key ("ID_DrugPrescription")
      references "tDrugsPrescriptions" ( "ID" )
        on delete restrict
        on update cascade;
ALTER TABLE "tExemptions"
  RENAME TO "tExemptionsAndMore";
ALTER TABLE "tExemptionsAndMore"
  Add column "Type" char(1);
update "tExemptionsAndMore"
  set "Type"='E';
alter table "tExemptionsAndMore"  
  alter column "Type" set not null;
alter table "tExemptionsAndMore"
  add constraint "ck_tExemptionsAndMore_Type"
    check ( "Type" in ('E','A','I','O') );
COMMENT ON COLUMN "tExemptionsAndMore"."Type"
  IS 'E = Exemption
A = Assurance
I = Health ID card or similar
O = Other (please, inform us about what you write in this case. Thank you!)';
alter table "tDrugs"
  add column "ID_MeasurementUnit" integer NOT NULL;
alter table "tDrugs"
  add constraint "fk_tDrugs_tMeasurementsUnits"
    foreign key ("ID_MeasurementUnit")
      references "tMeasurementsUnits" ("ID")
        on update cascade
        on delete restrict;
alter table "tDrugsPrescriptions"
  add column "Expired" boolean not null default false;
alter table "tDrugs"
add column "Highlight_Reason" character varying(45);
alter table "tDrugs"
add column "Highlight_Color" integer;
alter table "tLife"
add column "Highlight_Reason" character varying(45);
alter table "tLife"
add column "Highlight_Color" integer;
-- 
alter table "tLife"
add column "ID_Problem" integer;
alter table "tLife"
add constraint "fk_tLife_tProblems" 
  foreign key ("ID_Problem")
    references "tProblems" ("ID")
      on delete restrict on update cascade;
-- 
alter table "tExaminations"
add column "ID_Problem" integer;
alter table "tExaminations"
add constraint "fk_tExaminations_tProblems" 
  foreign key ("ID_Problem")
    references "tProblems" ("ID")
      on delete restrict on update cascade;
-- 
alter table "tProblems"
add column "NeedsRegularChecks" boolean not null default false;
-- 
alter table "tExaminations"
add column "ID_Pathology" integer;
alter table "tExaminations"
  add CONSTRAINT "fk_tExaminations_tPatientsPathologies" 
    FOREIGN KEY ("ID_Patient", "ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient", "ID_Pathology")
      ON UPDATE CASCADE ON DELETE RESTRICT;
alter table "tLife"
  add column "ID_Pathology" integer;
alter table "tLife"
  add CONSTRAINT "fk_tLife_tPatientsPathologies" 
    FOREIGN KEY ("ID_Patient", "ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient", "ID_Pathology")
      ON UPDATE CASCADE ON DELETE RESTRICT;
alter table "tDrugs"
  add column "ID_Problem" integer;
alter table "tDrugs"
  add CONSTRAINT "fk_tDrugs_tProblems" 
    FOREIGN KEY ("ID_Problem")
      REFERENCES "tProblems" ("ID")
      ON UPDATE CASCADE ON DELETE RESTRICT;
alter table "tDrugs"
  add column "ID_Pathology" integer;
alter table "tDrugs"
  add CONSTRAINT "fk_tDrugs_tPatientsPathologies" 
    FOREIGN KEY ("ID_Patient", "ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient", "ID_Pathology")
      ON UPDATE CASCADE ON DELETE RESTRICT;
alter table "tDrugsPrescriptions"
  add column "ID_Pathology" integer;
alter table "tDrugsPrescriptions"
  add CONSTRAINT "fk_tDrugsPrescriptions_tPatientsPathologies" 
    FOREIGN KEY ("ID_Patient", "ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient", "ID_Pathology")
      ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE "tDrugsPrescriptions"
   ALTER COLUMN "ID_Problem" DROP NOT NULL;
ALTER TABLE "tDrugsPrescriptions"
  add constraint "ck_tDrugsPrescriptions_ProblemOrPathology"
    check ( "ID_Pathology" is not null or "ID_Problem" is not null );
--  
alter table "tPrescriptions"
  add column "ID_Problem" integer;
alter table "tPrescriptions"
  add CONSTRAINT "fk_tPrescriptions_tProblems" 
    FOREIGN KEY ("ID_Problem")
      REFERENCES "tProblems" ("ID")
      ON UPDATE CASCADE ON DELETE RESTRICT;
alter table "tPrescriptions"
  add column "ID_Pathology" integer;
alter table "tPrescriptions"
  add CONSTRAINT "fk_tPrescriptions_tPatientsPathologies" 
    FOREIGN KEY ("ID_Patient", "ID_Pathology")
      REFERENCES "tPatientsPathologies" ("ID_Patient", "ID_Pathology")
      ON UPDATE CASCADE ON DELETE RESTRICT;
-- ----------
-- There is new default data
update "tImports" set "AvailableVersion"=2 where "CountryCode"='it';
update "tSettings"
set "DbVersion"=2
where "ID"=1;
